package juctest;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.*;


/**
 * @BelongsProject: Java8NewFeatures
 * @BelongsPackage: juctest
 * @Author: 江岸
 * @CreateTime: 2021-04-06 21:06
 * @Description: 线程池处理for循环
 */
public class PoolDealfor {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
        PoolDealfor poolDealfor = new PoolDealfor();
        long start = System.currentTimeMillis();
        poolDealfor.fun1(list);
        long end = System.currentTimeMillis();
        System.out.println("总时间 = " + (end - start));
    }

    public  void fun1(List<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            Integer integer = list.get(i);
            //需要模拟耗时操作才有用
            System.out.println("主线程"+"----"+integer);
        }
    }

    public  void fun2(List<Integer> list) {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < list.size(); i++) {
            Integer integer = list.get(i);
            Runnable run = new Runnable() {
                public void run() {
                    //需要模拟耗时操作才有用
                    System.out.println(Thread.currentThread().getName() + "----" + integer);
                }
            };
            pool.execute(run);
        }
        pool.shutdown();
    }

    public void ThreadPool(){
       new ThreadPoolExecutor(5,//核心线程池大小
               10,//最大线程池大小
               100,//线程最大空闲时间
               TimeUnit.SECONDS,//时间单位
               new LinkedBlockingQueue<Runnable>(30) //线程等待队列
       );
    }
}
